package com.person.leetcode.editor.cn;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
  * 160.相交链表
  * 2022-08-12 09:43:01
**/
public class Sequence_0160_IntersectionOfTwoLinkedLists {
    public static void main(String[] args) {
        Solution solution = new Sequence_0160_IntersectionOfTwoLinkedLists().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
//事实证明 两条链的重合处,抽象成一条有奇效
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode pA = headA, pB = headB;
        while (pA != pB) {
            //太妙了
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
    }
}


//leetcode submit region end(Prohibit modification and deletion)
}